﻿@using Website.CRUDServices
@using Website.Models.Styling
@using Location = Website.Models.Domain.Location
@model Website.Models.ViewModels.ListView<Cluster>
@{
    this.ViewBag.Title = "Klynger";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var methods = this.ViewData["ClusterMethods"] as List<ClusterMethod>;
    var statuses = this.ViewData["ClusterStatuses"] as List<ClusterStatus>;
    var locations = this.ViewData["Locations"] as List<Location>;
    var clusterMethodTypes = this.ViewData["ClusterMethodTypes"] as List<ClusterMethodType> ?? new List<ClusterMethodType>();
    var clusterStatusTypes = this.ViewData["ClusterStatusTypes"] as List<ClusterStatusType> ?? new List<ClusterStatusType>();
}

<div class="float-right smallMarginBottom">
    <input type="button" onclick="@string.Format("location.href = '{0}';", this.Url.Action("NewCluster"))" value="Opret ny" class="" />
</div>

<table class="tableView">
    <tr>
        @foreach (TableViewHeaderItem item in this.Model.Header.Items)
        {
            <td>
                @Html.Partial("~/Views/_Shared/TableViewHeader.cshtml", item)
            </td>
        }
        <td></td>
    </tr>
    @if (this.Model != null)
    {
        foreach (Cluster cluster in this.Model.List)
        {
            var thisMethodTypes = methods.Where(x => cluster.Methods.Contains(x.ID)).Select(x => x.Type).Where(x => x != Guid.Empty);
            var thisStatusTypes = statuses.Where(x => cluster.Statuses.Contains(x.ID) && x.IsActive).Select(x => x.Type).Where(x => x != Guid.Empty);
            List<Location> locs = locations.Where(x => x.ID == cluster.Location).ToList();
            Location location = locs.Any() ? locations[0] : null;

        <tr>
            <td>@string.Format("{0:dd-MM-yyyy}", cluster.Date)</td>
            <td>
                @if (location != null)
                {
                    @location.Name
                }

            </td>
            <td>@cluster.Name</td>
            <td>
                @if (thisMethodTypes.Any())
                {
                    @Html.Raw(string.Join(",<br/>", clusterMethodTypes.Where(x => thisMethodTypes.Contains(x.ID)).Select(x => x.Name)))
                }
            </td>
            <td>
                @if (thisStatusTypes.Any())
                {
                    @Html.Raw(string.Join(",<br/>", clusterStatusTypes.Where(x => thisStatusTypes.Contains(x.ID)).Select(x => x.Name)))
                }
            </td>
            <td><a href="@Url.Action("UpdateCluster", new { @Id = cluster.ID })">
                <img src="@Url.Content("~/Content/ButtonIcons/tool_blue.png")" alt="Link"/></a></td>
        </tr>
        }
    }
    <tr>
        @Html.Partial("~/Views/_Shared/PagingArrows.cshtml", new PagingArrows { Action = "Index", TotalAmountOfColumns = 6, CurrentSkip = this.Model.Skip, CurrentTake = this.Model.Take, CurrentTotalRowCount = this.Model.TotalRows })
    </tr>
</table>
